package com.top.horizon.th1804app.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.protocol.model.FileTag;
import com.top.horizon.th1804app.R;

import java.util.List;

public class TagLinearLayout extends LinearLayout {
    private final String TAG = TagLinearLayout.class.getSimpleName();
    private Context mContext;
    private int totalSize;
    private List<FileTag.Tag> tags;
    private int WIDTH = 60;

    public TagLinearLayout(Context context) {
        this(context, null);
    }

    public TagLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TagLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public void setTags(List<FileTag.Tag> tags) {
        this.tags = tags;
        addTag();
        invalidate();
    }

    private void addTag() {
        if (totalSize == 0) {
            Log.d(TAG, "totalSize == 0 不能添加tag");
            return;
        }
        removeAllViews();
        if (tags == null || tags.size() == 0) {
            Log.d(TAG, "没有标签可添加");
            return;
        }
        for (int i = 0; i < tags.size(); i++) {
            FileTag.Tag fileTag = tags.get(i);
            TextView textView = genTextView();
            textView.setTag(fileTag);
            textView.setText(" ");
            Drawable drawable = null;
            if (fileTag.selected) {
                drawable = mContext.getResources().getDrawable(R.mipmap.icon_tag_blue);
            } else {
                drawable = mContext.getResources().getDrawable(R.mipmap.tag);
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            textView.setCompoundDrawables(null, null, null, drawable);
            addView(textView);
        }
        invalidate();
    }

    private TextView genTextView() {
        TextView tag = new TextView(mContext);
        LayoutParams layoutParams = new LayoutParams(WIDTH, WIDTH);
        tag.setLayoutParams(layoutParams);
        return tag;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int measuredWidth = getMeasuredWidth();
        Log.i(TAG, "width = " + measuredWidth);
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            FileTag.Tag tag = (FileTag.Tag) childAt.getTag();
            int marginLeft = (int) (tag.positon * 1.0 / totalSize * measuredWidth - WIDTH / 2);
            childAt.layout(l + marginLeft, t, l + marginLeft + WIDTH, b);
        }
    }

    public void refresh() {
        invalidate();
    }
}
